package com.example.demo.model.meta;

import com.baidu.bas.annotation.Erupt;
import com.baidu.bas.annotation.EruptField;
import com.baidu.bas.annotation.sub_field.Edit;
import com.baidu.bas.annotation.sub_field.EditType;
import com.baidu.bas.annotation.sub_field.View;
import com.baidu.bas.upms.model.base.HyperModel;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

import javax.persistence.*;
import javax.persistence.Column;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 数据源
 * @author liuzhenchuan@foxmail.com
 * @date 2020/12/22
 */
@Setter
@Getter
@Erupt(name = "数据目录",orderBy = "id desc")
@Table(name = "ma_meta_logical_catalog",uniqueConstraints = {
        @UniqueConstraint(columnNames = "name")
})
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
@DynamicUpdate
@NoArgsConstructor
public class LogicalCatalog extends HyperModel {
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    protected String name;
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    protected String cname;
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.TEXTAREA)
    )
    protected String remarks;
    /**
     * 分组
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.TAGS)
    )
    @ElementCollection
    @CollectionTable(name = "ma_meta_catalog_tags", joinColumns = @JoinColumn(name = "id"))
    @Column(name = "tags")
    protected Set<String> tags;

    @OneToMany(mappedBy="logicalCatalog",cascade=CascadeType.ALL,fetch=FetchType.LAZY,orphanRemoval = true)
    List<LogicalTable> logicalTableList;


    public LogicalCatalog setLogicalTableList(List<LogicalTable> logicalTableList) {
        this.logicalTableList = logicalTableList;
        logicalTableList.forEach(it->{
            it.setLogicalCatalog(this);
        });
        return this;
    }

    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.BOOLEAN)
    )
    protected Boolean enable = true;

    public LogicalCatalog(String name) {
        this.name = name;
        this.cname = name;
    }

    public LogicalCatalog(String name, String cname) {
        this.name = name;
        this.cname = cname;
    }

}
